package com.wwwarehouse.taskcenter.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.PreviewWareCodeBean;

import java.util.ArrayList;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/20 13:40
 * @描述 ${TODO}
 */

public class PreviewWareCodeAdapter extends BaseAdapter {

    private Context                   mContext;
    private ArrayList<PreviewWareCodeBean.ListBean> mWareCodeList;
    private GridView                  mGridView;

    public  PreviewWareCodeAdapter(Context context, ArrayList<PreviewWareCodeBean.ListBean> wareCodeList, GridView gridView) {
        this.mContext = context;
        this.mWareCodeList = wareCodeList;
        this.mGridView = gridView;

    }
    @Override
    public int getCount() {
        return mWareCodeList == null ? 0 : mWareCodeList.size();
    }

    @Override
    public Object getItem(int position) {
        return mWareCodeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PreviewWareCodeBean.ListBean listBean = mWareCodeList.get(position);
        PreviewWareCodeAdapter.ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.preview_ware_child_item, null);
            holder = new PreviewWareCodeAdapter.ViewHolder();
            holder.mTvFloor = (TextView) convertView.findViewById(R.id.tv_floor);
            holder.mTvGrid = (TextView) convertView.findViewById(R.id.tv_grid);
            holder.mTvCode = (TextView) convertView.findViewById(R.id.tv_code);
            convertView.setTag(holder);
        } else {
            holder = (PreviewWareCodeAdapter.ViewHolder) convertView.getTag();
        }
        final View finalConvertView = convertView;
        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                int itemHeight = (mGridView.getMeasuredHeight() - mGridView.getPaddingTop() - mGridView.getPaddingBottom() - mGridView.getVerticalSpacing() * (5 - 1)) / (5);
                params.height = itemHeight;
                finalConvertView.setLayoutParams(params);
                mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        holder.mTvFloor.setText(listBean.getFloor()+" ");
        holder.mTvGrid.setText(listBean.getLattic());
        holder.mTvCode.setText(listBean.getDatacode());
        return convertView;

    }
    private class ViewHolder {
        private TextView mTvFloor;
        private TextView mTvGrid;
        private TextView mTvCode;
    }
}
